<?php

namespace app\wachat\controller;

use think\Controller;
use think\Lang;
use think\Request;

class Base extends Controller
{

    protected $template, $system, $datas,$resulr;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        //头部导航
        $navs = model('nav')->catenav($this->datas);

        $json_navs = json_encode($navs);

        //底部导航
        $nav = cache('nav' . cookie('think_var'));
        if (empty($nav)) {
            $nav = model('nav')
                ->where('pid', '0')
                ->where($this->datas)
                ->order('sort ASC,id ASC')
                ->limit(10)->select();
            cache('nav' . cookie('think_var'), $nav);
        }

        //在线帮助
        $online = cache('online' . cookie('think_var'));
        if (empty($online)) {
            $online = model('online')
                ->where($this->datas)
                ->select();
            cache('online' . cookie('think_var'), $online);
        }

        //幻灯片
        $navid = $this->request->param('navid');
        $navid = $navid ? $navid : '0';
        $carousel = cache('\'' . $navid . '\'' . cookie('think_var'));
        if (empty($carousel)) {
           $where['model'] = $this->request->module();
            isset($navid) ? $where['type'] = ['like', '%,' . $navid . ',%'] : '';
            $carousel = model('carousel')
                ->where($where)
                ->where($this->datas)
                ->select();
            cache('\'' . $navid . '\'' . cookie('think_var'), $carousel);
        }

        //幻灯片
        $centerturn = cache('turn\'' . $navid . '\'' . cookie('think_var'));
        if (empty($centerturn)) {
            $where = null;
            isset($navid) ? $where['type'] = ['like', '%,' . $navid . ',%'] : '';
            $centerturn = model('centerturn')->where($where)->where($this->datas)->order('sort deSC')->limit(4)->select();
            cache('turn\'' . $navid . '\'' . cookie('think_var'), $centerturn);
        }
        $this->assign([
            'carousel' => $carousel,
            'centerturn' => $centerturn,
            'online' => $online,
            'json_navs' => $json_navs,
            'nav' => $navs,
            'system' => cache('systemsystem' . cookie('think_var')),
        ]);
    }


    protected $beforeActionList = [
        'right' => ['only' => 'index,detailed'],
    ];

    public function index($navid)
    {

        if (!$navid) {
            $this->redirect($this->request->domain());
            //$this->error('参数错误');
        }
        /*栏目详情start*/
        $cateInfo = db('nav')->where('id', $navid)->where($this->datas)->find();
        if (!$cateInfo) {
            if(!empty($this->request->param('navid'))){
                $lang= model('nav')->where(['id'=>$this->request->param('navid')])->value('lang');
                if($lang){
                    cookie('think_var',$lang);
                    $this->redirect($this->request->url(true));
                }
            }
            $this->redirect($this->request->domain());
            //$this->error('栏目不存在');
        }
        /*栏目详情end*/
        $this->assign('cateInfo', $cateInfo);
        $this->assign('empty', lang('No relevant data'));

        /*模板start*/
        if ($cateInfo['type'] == 'one') {

            if (empty($cateInfo['pid'])){

                if(empty(cache('nav_top_one'.$navid. cookie('think_var')))) {
                    $cateInfo_new = db('nav')
                        ->where('pids', 'like', '%,' . $navid . ',%')
                        ->where($this->datas)
                        ->where('type','one')
                        ->where('id','neq',$navid)
                        ->order('sort deSC')
                        ->find();
                    cache('nav_top_one'.$navid. cookie('think_var'),$cateInfo_new);
                }

                if(cache('nav_top_one'.$navid. cookie('think_var'))){
                    $this->assign('cateInfo', cache('nav_top_one'.$navid. cookie('think_var')));
                }else{
                    $cateInfo_new = db('nav')
                        ->where('pids', 'like', '%,' . $navid . ',%')
                        ->where($this->datas)
                        ->where('type','model')
                        ->where('id','neq',$navid)
                        ->order('sort deSC')
                        ->find();
                    if(!empty($cateInfo_new)){
                        return $this->index($cateInfo_new['id']);
                    }
                }

            }

            /*文章列表end*/
            return $this->fetch($this->template . $cateInfo['temppage']);
        } else {
            if ($cateInfo['pid']) {
                /*所有数据列表start*/
                $list = db($cateInfo['model'])->alias('a')
                    ->join('__NAV__ c', 'c.id= a.cid')
                    ->field('a.*,c.name,c.model')
                    ->where('c.status', '1')
                    ->where('a.status', '1')
                    ->where('a.lang = c.lang')
                    ->where('a.lang',cookie('think_var'))
                    ->order('a.sort DESC,a.id DESC')
                    ->where('c.id', $navid)
                    ->paginate()
                    ->appends($this->request->param());
            } else {
                /*列表start*/
                $list = db($cateInfo['model'])->alias('a')
                    ->join('__NAV__ c', 'c.id = a.cid')
                    ->field('a.*,c.name,c.model')
                    ->where('c.status', '1')
                    ->where('c.model', $cateInfo['model'])
                    ->where('a.status', '1')
                    ->where('a.lang = c.lang')
                    ->where('a.lang',cookie('think_var'))
                    ->order('a.sort DESC,a.id DESC')
                    ->where('c.pids', 'like', '%,' . $navid . ',%')
                    ->paginate()
                    ->appends($this->request->param());
            }
            $this->assign('list', $list);
            /*列表end*/
            return $this->fetch($this->template . $cateInfo['templist']);
        }
        /*模板end*/
    }


    public function detailed($id)
    {
        //显示内容
        $model = $this->request->controller();

        $data = model($model)->where($this->datas)->find($id);

        $cateInfo = db('nav')->where('id', $data['cid'])->where($this->datas)->find();
        if (!$cateInfo) {
            $this->error(Lang::get('error'));
        }
        //更多页面
        !empty($data['cid']) ? $datas['cid'] = $data['cid'] : '';
        $datas['status'] = '1';
        $datas['lang'] = cookie('think_var');

        $next = model($model)->getnext($id, $datas);
        $this->resulr=[
            'data'=>$data,
            'cateInfo'=>$cateInfo,
            'next'=>$next
        ];
        echo json_encode($this->resulr);
        exit();
        $this->assign('data', $data);
        $this->assign('cateInfo', $cateInfo);
        $this->assign('next', $next);
//        return $this->fetch($this->template . $cateInfo['temparticle']);
    }

    /**
     * 更多页面
     */
    public function more(){
        $table = $this->request->controller();
        $list=model($table)->where($this->datas)->paginate();
        $reight_nav = model('nav')->where('model', $table)->where($this->datas)->select();
        $this->assign('list', $list);
        $this->assign('reight_nav', $reight_nav);
        $this->assign('fname_nav', Lang::get('All class'));
        $this->assign('empty', lang('No relevant data'));
        return $this->fetch($this->template.'more.html');
    }

    //左边导航
    public function right()
    {
        $pid = model('nav')->where('id', $this->request->param('navid'))->where($this->datas)->value('pid');
        $pid = !empty($pid) ? $pid : $this->request->param('navid');
        $reight_nav = model('nav')
            ->where('pid', $pid)
            ->where($this->datas)
            ->order('sort asc,id ASC')
            ->select();

        $id = !empty($reight_nav[0]['pid']) ? $reight_nav[0]['pid'] : $this->request->param('navid');
        $fname_nav = model('nav')->where('id', $id)->where($this->datas)->value('name');

        $this->assign('reight_nav', $reight_nav);
        $this->assign('fname_nav', $fname_nav);
    }

}
